package com.controller;

import com.pojo.Admin;
import com.pojo.Member;
import com.pojo.Product;
import com.pojo.Result;
import com.server.AdminService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description：管理员管理
 */
@Controller
@RequestMapping("ad")
public class AdController {
    @Resource
    AdminService adminService;

    @RequestMapping(value = "/competence",method = RequestMethod.GET)
    public String getAdd(String name,String description,Model model){
        List<Admin>addList=adminService.getAdd(name,description);
        model.addAttribute("addList",addList);
        return "pages/admin_Competence" ;
    }


    @RequestMapping(value ="/administrator",method = RequestMethod.GET)
    public String getadminList(Model model){
        List<Admin> adminList=adminService.getadminList();
        model.addAttribute("adminList",adminList);
        return "pages/administrator" ;
    }

    @RequestMapping(value ="/admin_info",method = RequestMethod.GET)
    public String index3(){
        return "pages/admin_info" ;
    }


    /*"name":name,"password":password,"sex":sex,"age":age,"phone":phone,"rolemid":rolemid*/
    @RequestMapping(value = "/addadmin",method = RequestMethod.GET)
    @ResponseBody
    public Result addadmin(@Param("name") String name,@Param("password") String password, @Param("age")int age,
         @Param("sex")String sex,@Param("phone")String phone, @Param("rolename")String rolename,Model model, HttpSession session){
        System.out.println();
        Md5Hash jiamipassword = new Md5Hash(password, name, 1024);
        int count= adminService.addadmin(name, String.valueOf(jiamipassword), age, sex, phone,rolename);

        Result result = new Result();
        if (count>0){
            List<Admin> adminList=adminService.getadminList();
            model.addAttribute("adminList",adminList);
            session.setAttribute("adminList",adminList);
            result.setSuccess(true);
        }else {
            result.setMsg("添加失败");
        }
        return result;
    }
    @RequestMapping(value ="/edit",method = RequestMethod.GET)
    @ResponseBody
    public Result updateAdmin(@Param("adminid") Integer adminid, Model model, HttpSession session){
        Result result = new Result();
        if (session.getAttribute("admin1")!=null){
            session.removeAttribute("admin1");
        }

        Admin admin1=adminService.selectAdminById(adminid);
        System.out.println(admin1);

        session.setAttribute("admin1",admin1);

        if (admin1!=null){
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);

        return result;

    }
    @RequestMapping(value ="/updateAdmin",method = RequestMethod.GET)
    @ResponseBody
    public Result updateAdmin(@Param("adminid") int adminid,@Param("name")String name,@Param("password") String password,
      @Param("age")int age,@Param("sex")String sex,@Param("phone")String phone,@Param("rolename")String rolename,HttpSession session){

        Result result = new Result();

        int count=adminService.updateAdmin(adminid, name, password, age, sex, phone,rolename);
        session.removeAttribute("admin1");
        if (count>0){
            System.out.println("修改用户成功");
            result.setSuccess(true);
            return result;
        }
        System.out.println("修改用户失败");
        result.setSuccess(false);
        return  result;
    }
    @RequestMapping("/deleteByID")
    public String deleteByID(int adminid)
    {
        /*int result = adminService.deleteById(Integer.valueOf(adminid));*/
        adminService.deleteById(adminid);

        return "/pages/administrator";
    }
    /*搜索*/
    @RequestMapping("/getsearch")
    @ResponseBody
    public ModelAndView getsearch(String name) {
        //1.调用service方法
        List<Admin> adminList = adminService.getsearch(name);
        //2.实例化一个ModelAndView对象
        ModelAndView mv=new ModelAndView();
        mv.addObject("adminList",adminList);
        mv.setViewName("/pages/administrator");
        return mv;
    }

    /*@RequestMapping("/addByID")
    public String addByID(String name)
    {
        *//*int result = adminService.deleteById(Integer.valueOf(adminid));*//*
        adminService.addByID(name);

        return "redirect:pages/administrator";
    }
    @RequestMapping("/allcount")
    public String allcount(Model model)
    {
        *//*int result = adminService.deleteById(Integer.valueOf(adminid));*//*
        *//*int allcount = adminService.allcount(1);*//*
        int allcount = adminService.getallcount();
        model.addAttribute("allcount",allcount);
        return "pages/administrator";
    }*/

}
